<?php
/** 
 * display information on debug screen
 */
defined ( 'IN_SYS' ) or die ( 'Restricted Access' );
class SysDebug {
	function printDebug () {
		$html = "\n".'<div class="system_debug" style="font-size: 0.8em;">';
		if (defined('DEBUG_SHOW_ERROR') && DEBUG_SHOW_ERROR === true) {
			$html.= $this->getError();
		}
		
		if (defined('DEBUG_SHOW_SQL') && DEBUG_SHOW_SQL === true) {
			$html.= $this->getSql();
		}
		
		if (defined('DEBUG_SHOW_INFO') && DEBUG_SHOW_INFO === true) {
			$html.= $this->getInfo();
		}
		
		if (defined('DEBUG_SHOW_SESSION') && DEBUG_SHOW_SESSION === true) {
			$html.= $this->getSess();
		}
		
		if (defined('DEBUG_SHOW_INCLUDE') && DEBUG_SHOW_INCLUDE === true) {
			$html.= $this->getInc();
		}
		
		if (defined('DEBUG_SHOW_PARAM') && DEBUG_SHOW_PARAM === true) {
			$html.= $this->getParameter();
		}
		
		if (defined('DEBUG_SHOW_LOG') && DEBUG_SHOW_LOG === true) {
			$html.= $this->getUpdateLog();
		}
		
		if (Page::has('SYS_TIME_END')) {
			Page::set('SYS_TIME_END', sys_get_microtime());
		}
		$time_total = Page::get('SYS_TIME_END') - Page::get('SYS_TIME_BEGIN');
		$html.= '<div style="font-size: 0.8em;font-style:italic;">Run time of Page (s): ' . $time_total . '</div>';
		$html.= '</div>';
		
		echo $html;
	}
	
	
	
	/**
	 * return error
	 */
	private function getError () {
		$errors = SysLogs::getLogError();
		$html = '';	
		if (!empty($errors)) {
			$html = "<div><span class=\"system_title\"> ERROR =</span> <ol>";
	  		foreach ($errors as $rs){
				$html .= '<li class="system_error_row">'.nl2br($rs)."</li>\n";
			}
			$html .= "</ol></div>\n";
		}
		
		return $html;
	}
	
	/**
	 * return sql query
	 */
	private function getSql () {
		$sqls = SysLogs::getLogSql();
		$html = '';
		if (!empty($sqls)) {
			include_once SYS_LIB_KERNEL . 'highlightSql.php';
			$highligh = new highlightSql();
			$html = '<div><span class="system_title">SQL:</span> <ol>';
	  		foreach ($sqls as $rs){
				$html .= '<li>'.nl2br($highligh->highlight($rs['sql_end']))."\n";
				$html .= '<br/><span class="run_time">Run time (s): '.($rs['time_end'] - $rs['time_begin'])."</span></li>";
			}
			$html .= "</ol>\n";
			$html .= '</div>';
		}
		
		return $html;
	}
	
	/**
	 * return infomation
	 */
	private function getInfo () {
		$html = '<div><pre><span class="system_title">PAGE-INFO:</span> '."\n".var_export(Page::getInfoPage(), true).'</pre></div>';
		return $html;
	}
	
	/**
	 * return include file
	 */
	private function getInc () {
		$vars = get_included_files();
		$html = "<ol>";
  		foreach ($vars as $rs){
			$html .= '<li>'.str_replace(substr(SYS_ROOT, 0, -1), '', $rs)."</li>";
		}
		$html .= "</ol>\n";
		return $html;
	}
	
	private function getSess () {
		if (SYSTEM_MULTI_CPU) {
			$name = 'SESSION';
		} else {
			$name = 'SESSION';
		}
		$html = '<div><pre><span class="system_title">'.$name.':</span> '."\n".var_export(Page::getSessionAll(), true).'</pre></div>';
		
		if (!SYSTEM_MULTI_CPU && Page::get('SYSTEM_LAST_SESSION') != $_SESSION) {
			$html.= '<div><pre><span class="system_title">LAST_SESSION:</span> '."\n".var_export(Page::get('SYSTEM_LAST_SESSION'), true).'</pre></div>';
		}
		
		return $html;
	}
	
	/**
	 * for debug
	 */
	private function getParameter () {
		$html = '';
		if (!empty(Page::$page_param)) {
			$output = "array(\n";
			$i = 0;
			foreach (Page::$page_param as $param) {
				$output.= "\t[{$i}] => array(\n";
				foreach ($param as $key=>$val) {
					$output.= "\t\t'{$key}' \t=> [{$val}]\n";
				}
				$output.= "\t),\n";
				$i++;
			}
			$output.= ")";
			$html .= '<div><pre><span class="system_title">PARAMETER:</span> '."\n".$output.'</pre></div>';
		}
		return $html;
	}
	
	
	private function getUpdateLog() {
		$file = SYS_CONFIG . 'update.log';
		if (!file_exists($file)) return;
		
		$content = '<span class="system_title">'.$file.'</span>'."\n\n";
		$content.= htmlDisplay(@file_get_contents($file));
		return '<div><pre>' . $content . '</pre></div>';
	}
}